﻿Imports HMSLibrary
Imports Microsoft.ApplicationBlocks.Data
Imports System.Data
Imports HMSDataLayer

Public Class ItemsServiceControl
    Implements IHMSData

    Public Property ActionMode As HMSLibrary.FormAction Implements HMSLibrary.IHMSData.ActionMode
    Public Property ID As Integer Implements HMSLibrary.IHMSData.ID

    Public Function DataValidation() As Boolean Implements HMSLibrary.IHMSData.DataValidation
        DataValidation = False
        Try
            If Trim(ItemNameTextBox.Text) = "" Then
                CommonMessages.ShowValidationMsg("Please enter name!")
                Exit Function
            End If
            If AppCommon.loginInfo.isItemNoAuto = False Then
                If Trim(ItemCodeTextBox.Text) = "" Then
                    CommonMessages.ShowValidationMsg("Please enter code!")
                    Exit Function
                End If
            End If
            If Trim(UOMComboBox.Text) = "" Then
                CommonMessages.ShowValidationMsg("Please select unit!")
                Exit Function
            End If

            If ActionMode = FormAction.ADD Then
                Dim strSql As String
                strSql = "SELECT  [ItemNo] FROM dbo.ItemServiceMaster WHERE  (Name = N'" & ItemNameTextBox.Text.Trim & "') "
                Dim objTemp As Object = Nothing
                objTemp = AppCommon.db.ExecuteScalar(CommandType.Text, strSql)
                If objTemp IsNot Nothing Then
                    If IsDBNull(objTemp) = False Then
                        CommonMessages.ShowValidationMsg("Item already exist!")
                        Exit Function
                    End If
                End If
            End If
            DataValidation = True
        Catch ex As Exception
            AppCommon.LogError(ex)
        End Try
    End Function

    Public Sub LoadData(ByVal ID As Integer) Implements HMSLibrary.IHMSData.LoadData
        ActionMode = FormAction.EDIT
    End Sub

    Public Function SaveData() As Boolean Implements HMSLibrary.IHMSData.SaveData
        If DataValidation() = False Then Return False
        Dim DataObject As New MasterDataLayer()
        Dim item As New ItemsService
        item.ItemCode = ItemCodeTextBox.Text
        item.Name = ItemNameTextBox.Text
        item.Description = RemarkTextBox.Text
        item.IsInventoryItem = IsInventoryItemCheckBox.IsChecked
        item.Rate = Val(RateTextBox.Text)
        item.UOM = UOMComboBox.SelectedValue
        item.Category = CategoryComboBox.SelectedValue
        item.SubCategory = SubCategoryComboBox.SelectedValue
        item.isItemNoAuto = AppCommon.loginInfo.isItemNoAuto
        item.ROL = Val(ROLTextBox.Text)
        DataObject.SaveItem(item, ActionMode)
        Return True
    End Function

    Private Sub ItemsUserControl_Loaded(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles Me.Loaded
        Dim dataProvider As New MasterDataLayer()
        Dim lst As List(Of Lookups)
        lst = dataProvider.GetLookupList(LookupTypes.ItemCategory)
        CategoryComboBox.ItemsSource = lst
        SubCategoryComboBox.ItemsSource = lst

        lst = dataProvider.GetLookupList(LookupTypes.UOM)
        UOMComboBox.ItemsSource = lst
        ActionMode = FormAction.ADD
        If AppCommon.loginInfo.isItemNoAuto = True Then
            ItemCodeTextBox.IsReadOnly = True
        End If
    End Sub

    Public Sub AddNew() Implements HMSLibrary.IHMSData.AddNew
        ActionMode = FormAction.ADD
        ItemCodeTextBox.Text = String.Empty
        ItemNameTextBox.Text = String.Empty
        RemarkTextBox.Text = String.Empty
        IsInventoryItemCheckBox.IsChecked = False
        RateTextBox.Text = 0
        UOMComboBox.SelectedIndex = -1
        CategoryComboBox.SelectedIndex = -1
        SubCategoryComboBox.SelectedIndex = -1
        ROLTextBox.Text = 0
    End Sub

    Public Sub FindData() Implements HMSLibrary.IHMSData.FindData
        MainGrid.Visibility = Visibility.Collapsed
        FindStackPanel.Visibility = Visibility.Visible
        Dim DataObject As MasterDataLayer
        Dim DataCollection As Object
        DataObject = New MasterDataLayer()
        DataCollection = DataObject.GetItemServiceList
        SearchDataGrid.ItemsSource = DataCollection
    End Sub

    Public Sub BackView() Implements HMSLibrary.IHMSData.BackView
        MainGrid.Visibility = Visibility.Visible
        FindStackPanel.Visibility = Visibility.Collapsed
    End Sub

    Private Sub DeleteButton_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs)

    End Sub
End Class
